package com.mzplayer.widget;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.mzplayer.utils.RS;

@SuppressLint("ViewConstructor")
public class FloatContainer extends FrameLayout {

    private boolean isShowing;
    private final WindowManager windowManager;
    private final WindowManager.LayoutParams wParams;
    private final View child;

    public static boolean canFloat(final Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(context)) {
                AlertDialog alertDialog = new AlertDialog.Builder(context)
                        .setMessage("小窗播放视频需要在应用设置中开启悬浮窗权限,是否前往开启权限?")
                        .setNegativeButton("否", null)
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(intent);
                            }
                        }).create();
                alertDialog.show();
                int color = context.getColor(RS.color.ctl_fore);
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
                alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
                return false;
            }
        }
        return true;
    }

    public FloatContainer(View child) {
        super(child.getContext());
        windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            wParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            wParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        } else {
            wParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        wParams.gravity = Gravity.START | Gravity.TOP;
        wParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        wParams.format = PixelFormat.RGBA_8888;
        wParams.windowAnimations = 0;
        this.child = child;
    }

    public FloatContainer setSize(int w, int h) {
        wParams.width = w;
        wParams.height = h;
        return this;
    }

    public FloatContainer setOffset(int x, int y) {
        wParams.x = x;
        wParams.y = y;
        return this;
    }

    public int getOffsetX() {
        return wParams.x;
    }

    public int getOffsetY() {
        return wParams.y;
    }

    public void create() {
        if (!isShowing) {
            ViewGroup parent = (ViewGroup) child.getParent();
            if (parent != null) {
                parent.removeView(child);
            }
            addView(child,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            windowManager.addView(this, wParams);
            isShowing = true;
        }
    }

    public void flushOffset(int x, int y) {
        setOffset(x, y);
        if (isShowing) {
            windowManager.updateViewLayout(this, wParams);
        }
    }
    public void flushSize(int w, int h) {
        setSize(w, h);
        if (isShowing) {
            windowManager.updateViewLayout(this, wParams);
        }
    }
    public void dismiss() {
        if (!isShowing) {
            return;
        }
        removeView(child);
        windowManager.removeView(this);
        isShowing = false;
    }

}
